//增删管（数据，剪切板,屏幕,网络，(允许,关闭，询问,白名单(同账号下的设备，好友，))）
import 'package:eshi/app/CommonFunctions.dart';
import 'package:eshi/app/shi/data/Config.dart';
import 'package:flutter/material.dart';

class DevicesManage extends StatefulWidget{
  const DevicesManage({super.key});

  @override
  State<StatefulWidget> createState() => DevicesManageState();

}

class DevicesManageState extends State<DevicesManage>{
  List<String>  data = Config.bindDevices==null ? []: Config.bindDevices.split(",");
  bool get isNetOpen => false;

  updateData(){
    Config.bindDevices = data.join(",");
    setState(() {

    });
  }

  Widget FlowRow({required List<Widget> chldren}){
      return Wrap(
              // 对齐方式（左对齐、居中、右对齐等）
              alignment: WrapAlignment.start,
              // 主轴方向的间距（水平方向）
              spacing: 8.0,
              // 交叉轴方向的间距（垂直方向，换行后的行间距）
              runSpacing: 1.0,
              children: chldren,
    );
  }
  Widget conf(name){
        return   DropdownButton(
          hint: Text(name),
          value: null, 
          items: [
            DropdownMenuItem(value: 0, child: Text('开启')),
            DropdownMenuItem(value: 1, child: Text('关闭')),
            DropdownMenuItem(value: 2, child: Text('询问')),
            DropdownMenuItem(value: 3, child: Text('白名单')),
          ],
          onChanged: (value) {

          },
        );
  }
  @override
  Widget build(BuildContext context) {
    return Column(
        children: [
            Row(mainAxisAlignment: MainAxisAlignment.end, children: [
              FloatingActionButton(shape: CircleBorder(),child: Icon(Icons.add),onPressed: (){
                  prompt("", (v){
                     data.add(v);
                     updateData();
                  });
              }),
              SizedBox(width: 20,),
            ]),
            if(!isNetOpen)
              FlowRow(chldren:[
                for(var name in data)
                  TextButton(onPressed: (){
                      data.remove(name);
                      updateData();
                  }, child: Text(name))
              ]),
            if(isNetOpen)
                for(var i=0,name=data[i];i<data.length;i++)
                ConstrainedBox(
                  constraints: const BoxConstraints(
                    maxWidth: 500, // 最大宽度限制
                    minWidth: 100, // 可选：最小宽度
                  ),
                  child:Row(children: [
                    Text("${i+1}. "),
                    Text(name),
                    Expanded(child: SizedBox(width: 20,),),
                    // conf("数据"),
                    // conf("剪切板"),
                    // conf("屏幕"),
                    // conf("网络"),
                    IconButton(icon: Icon(Icons.delete),onPressed: (){
                      data.remove(name);
                      updateData();
                    })
                  ],)
                ),
        ],
    );
  }

}